<template>
  <div class="app-container" v-loading="loading">
    <el-form :inline="true" :model="queryParams">
      <div style="display: flex;flex-wrap: wrap">
        <el-col :span="4">
          <el-form-item>
            <el-input v-model="queryParams.receiptCode" placeholder="入库单号" clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-input v-model="queryParams.purchaseCode" placeholder="关联采购订单号" clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-select
                clearable
                placeholder="订单类型"
                style="width: 100%"
                v-model="queryParams.receiptType"
            >
              <template v-for="(item,index) in receipt_type" :key="index">
                <el-option :label="item.label" :value="item.value"></el-option>
              </template>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-select
                clearable
                placeholder="上架库位"
                style="width: 100%"
                v-model="queryParams.locationId"
            >
              <template v-for="(item,index) in locationList" :key="index">
                <el-option :label="item.label" :value="item.value"></el-option>
              </template>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-input v-model="queryParams.skuCode" placeholder="SKU编码" clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-input v-model="queryParams.skuName" placeholder="商品名称" clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-button icon="Search" type="primary" @click="getList">查询</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </div>
    </el-form>

    <vxe-table
        border
        style="margin-top: 10px;"
        align="center"
        :data="tableList"
        :height="tableHeight"
        :checkbox-config="{ trigger:'row'}"
        :row-config="{isCurrent: true, isHover: true}"
    >
      <vxe-column type="checkbox" width="60"></vxe-column>
      <vxe-column type="seq" width="60" title="序号"></vxe-column>
      <vxe-column field="receiptCode" width="180" title="入库单号"></vxe-column>
      <vxe-column field="purchaseCode" width="180" title="关联采购订单号"></vxe-column>
      <vxe-column field="receiptType" width="150"  title="订单类型">
        <template v-slot="{row}">
          {{ returnDictLabel(receipt_type, row.receiptType) }}
        </template>
      </vxe-column>
      <vxe-column field="skuCode" width="200"  title="SKU编码"></vxe-column>
      <vxe-column field="skuName" width="240" title="商品名称"></vxe-column>
      <vxe-column field="coverUrl" width="180" title="商品图片">
        <template #default="{row}">
          <ImagePreview v-if="row.coverUrl" :width="150" :height="50" :src="row.coverUrl" ></ImagePreview>
        </template>
      </vxe-column>
      <vxe-column field="companyName"  width="150" title="公司名称" ></vxe-column>
      <vxe-column field="warehouseName" width="150" title="仓库名称"></vxe-column>
      <vxe-column field="locationCode" width="180" title="上架库位"></vxe-column>
      <vxe-column field="remainPutawayNum" width="100" title="待上架数量"></vxe-column>
      <vxe-column field="totalPutawayNum" width="120" title="实际上架数量">
        <template #default="{row}">
          {{row.putawayNum}}/{{row.totalPutawayNum}}
        </template>
      </vxe-column>

    </vxe-table>
    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

  </div>

</template>

<script setup>
import {getCurrentInstance, nextTick, onMounted, ref} from "vue";
import {getLocationFilter, getReceiptPutawayList} from "@/api/scApi";
import ImagePreview from "@/components/ImagePreview/index.vue";

const {proxy} = getCurrentInstance();

const { receipt_type } = proxy.useDict("receipt_type");


let loading=ref(false)
const queryParams = ref({
  pageNum: 1,
  pageSize: 10
})
let total = ref(0)
let tableHeight = ref(0);
let locationList=ref([]) //库位集合

nextTick(() => {
  tableHeight.value = window.innerHeight - 350;
})
window.onresize = () => {
  tableHeight.value = document.body.scrollHeight - 350;
}

onMounted(() => {
  getList()
  getLocationList()
})

let tableList = ref([])

function getList() {
  loading.value=true
  getReceiptPutawayList({
    pageNum: queryParams.value.pageNum,
    pageSize: queryParams.value.pageSize,
    param: queryParams.value
  }).then(res => {
    loading.value=false
    if (res.code === 200) {
      tableList.value = res.data
      total.value=res.total
    }
  }).finally(()=>{
    loading.value = false
  })
}

const resetQuery = () => {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
  }
  getList();
};

//获取库位
function getLocationList(){
  getLocationFilter({status:0}).then(res=>{
    if(res.code===200){

      locationList.value= res.data.map(item => {
        item.value = item.locationId
        item.label = item.locationCode
        return item
      })
    }
  })
}

</script>

<style scoped lang="scss">
.el-form-item {
  width: 90%;
}
</style>